package model.map;

import java.util.ArrayList;
import java.util.HashSet;

import main.Log;
import model.Model;
import utilities.*;

/**
 * A class that holds the location of objects on the map.
 * The class has the equals and hashcode functions overridden to give equality on x and y coordinates being equal.
 */
public class Location implements Archiveable {

	private int x;
	private int y;
	
	/* --------------------------------------------------------------------------------
	 * Constructors
	   -------------------------------------------------------------------------------- */
	
	public Location(){}
	
	/**
	 * @param x
	 * @param y
	 */
	public Location(int x, int y) 
	{
			this.x = x;
			this.y = y;
	}
	
	/* --------------------------------------------------------------------------------
	 * Getters and Setters
	   -------------------------------------------------------------------------------- */

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	/* --------------------------------------------------------------------------------
	 * Class methods
	   -------------------------------------------------------------------------------- */
	
	/**
	 * Sets a new location for an object
	 * @param x
	 * @param y
	 */
	public void setLocation(int x, int y)
	{
			this.x = x;
			this.y = y;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Location other = (Location) obj;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}

	@Override
	public String toString()
	{
		return "Location [x=" + x + ", y=" + y + "]";
	}
	
	public Archive save() {
		Archive archive = new JSONArchive();

		archive.putAttribute("x", Integer.toString(this.x));
		archive.putAttribute("y", Integer.toString(this.y));
		
		return archive;
	}
	
	public void load(Archive archive) {
		this.x = Integer.parseInt(archive.getAttribute("x"));
		
		this.y = Integer.parseInt(archive.getAttribute("y"));
	}
	
}
